const db = require('../config/database');

class InboundDocument {
  constructor(id, goodsId, supplier, quantity, expectedArrivalDate, actualArrivalDate, status, operator, notes, createdAt) {
    this.id = id;                            // 单据编号
    this.goodsId = goodsId;                  // 关联货物ID
    this.supplier = supplier;                // 供应商
    this.quantity = quantity;                // 数量
    this.expectedArrivalDate = expectedArrivalDate;  // 预计到达时间
    this.actualArrivalDate = actualArrivalDate;      // 实际到达时间
    this.status = status;                    // 状态 (pending, received, completed, cancelled)
    this.operator = operator;                // 操作员
    this.notes = notes;                      // 备注
    this.createdAt = createdAt || new Date(); // 创建时间
  }

  // 保存入库单据到数据库
  save(callback) {
    const query = `
      INSERT INTO inbound_documents 
      (goods_id, supplier, quantity, expected_arrival_date, actual_arrival_date, status, operator, notes)
      VALUES (?, ?, ?, ?, ?, ?, ?, ?)
    `;
    const params = [
      this.goodsId,
      this.supplier,
      this.quantity,
      this.expectedArrivalDate,
      this.actualArrivalDate,
      this.status,
      this.operator,
      this.notes
    ];

    db.run(query, params, function(err) {
      if (err) {
        callback(err, null);
      } else {
        callback(null, { id: this.lastID });
      }
    });
  }

  // 更新单据状态
  updateStatus(newStatus, callback) {
    const query = `UPDATE inbound_documents SET status = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?`;
    db.run(query, [newStatus, this.id], function(err) {
      if (err) {
        callback(err, null);
      } else {
        callback(null, { changes: this.changes });
      }
    });
  }

  // 完成入库
  complete(actualQuantity, arrivalDate, callback) {
    this.actualArrivalDate = arrivalDate;
    this.quantity = actualQuantity;
    this.status = 'completed';
    
    const query = `
      UPDATE inbound_documents 
      SET actual_arrival_date = ?, quantity = ?, status = ?, updated_at = CURRENT_TIMESTAMP 
      WHERE id = ?
    `;
    db.run(query, [arrivalDate, actualQuantity, 'completed', this.id], function(err) {
      if (err) {
        callback(err, null);
      } else {
        callback(null, { changes: this.changes });
      }
    });
  }

  // 取消入库
  cancel(reason, callback) {
    this.status = 'cancelled';
    this.notes = reason;
    
    const query = `UPDATE inbound_documents SET status = ?, notes = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?`;
    db.run(query, ['cancelled', reason, this.id], function(err) {
      if (err) {
        callback(err, null);
      } else {
        callback(null, { changes: this.changes });
      }
    });
  }

  // 根据ID获取入库单据
  static findById(id, callback) {
    const query = `SELECT * FROM inbound_documents WHERE id = ?`;
    db.get(query, [id], (err, row) => {
      if (err) {
        callback(err, null);
      } else {
        callback(null, row);
      }
    });
  }

  // 获取所有入库单据
  static findAll(callback) {
    const query = `SELECT * FROM inbound_documents ORDER BY created_at DESC`;
    db.all(query, (err, rows) => {
      if (err) {
        callback(err, null);
      } else {
        callback(null, rows);
      }
    });
  }
}

module.exports = InboundDocument;